#ifndef CALIBRATEPARAM_H
#define CALIBRATEPARAM_H

#include "jsonconvert.h"
#include <QSettings>
#include "calibparam.h"


class CalibrateParam : public JsonConvert
{
public:
    CalibrateParam(QString sn) : m_sn_num(sn){}

public slots:
    void fromCalibParam(const CalibParam& m_cali)
    {
        this->m_fAreaSize = m_cali.m_fAreaSize;
        this->m_fXOffSet = m_cali.m_fXOffSet;
        this->m_fYOffSet = m_cali.m_fYOffSet;
        this->m_fAreaAngle = m_cali.m_fAreaAngle;
        this->m_iMirrorToX = m_cali.m_iMirrorToX;
        this->m_bMirror1Invert = m_cali.m_bMirror1Invert;
        this->m_fMirror1Scale = m_cali.m_fMirror1Scale;
        this->m_fMirror1Barrel = m_cali.m_fMirror1Barrel;
        this->m_fMirror1ladder = m_cali.m_fMirror1ladder;
        this->m_bMirror2Invert = m_cali.m_bMirror2Invert;
        this->m_fMirror2Scale = m_cali.m_fMirror2Scale;
        this->m_fMirror2Barrel = m_cali.m_fMirror2Barrel;
        this->m_fMirror2ladder = m_cali.m_fMirror2ladder;
        this->m_bUseCorFile = m_cali.m_bUseCorFile;
        this->m_strCorFileName = m_cali.m_strCorFileName;
        this->mK_value = m_cali.mK_value;
    }

    CalibParam toCalibParam()
    {
        CalibParam mCalibParam;
        mCalibParam.m_fAreaSize = this->m_fAreaSize;
        mCalibParam.m_fXOffSet = this->m_fXOffSet;
        mCalibParam.m_fYOffSet = this->m_fYOffSet;
        mCalibParam.m_fAreaAngle = this->m_fAreaAngle;
        mCalibParam.m_iMirrorToX = this->m_iMirrorToX;
        mCalibParam.m_bMirror1Invert = this->m_bMirror1Invert;
        mCalibParam.m_fMirror1Scale = this->m_fMirror1Scale;
        mCalibParam.m_fMirror1Barrel = this->m_fMirror1Barrel;
        mCalibParam.m_fMirror1ladder = this->m_fMirror1ladder;
        mCalibParam.m_bMirror2Invert = this->m_bMirror2Invert;
        mCalibParam.m_fMirror2Scale = this->m_fMirror2Scale;
        mCalibParam.m_fMirror2Barrel = this->m_fMirror2Barrel;
        mCalibParam.m_fMirror2ladder = this->m_fMirror2ladder;
        mCalibParam.m_fMirror1Parallelogram = 0;//镜片1四边形失真系数
        mCalibParam.m_fMirror2Parallelogram = 0;//镜片2四边形失真系数
        mCalibParam.m_bUseCorFile = this->m_bUseCorFile;
        mCalibParam.m_strCorFileName = this->m_strCorFileName;
        mCalibParam.mK_value = this->mK_value;
        return mCalibParam;
    }

    void readFromFile()
    {
        QSettings settings("config.ini", QSettings::IniFormat);
        settings.beginGroup(QString("CalibrateParam_%1").arg(m_sn_num));
        this->m_fAreaSize = settings.value("m_fAreaSize", 200).toDouble();
        this->m_fXOffSet = settings.value("m_fXOffSet", 0).toDouble();
        this->m_fYOffSet = settings.value("m_fYOffSet", 0).toDouble();
        this->m_fAreaAngle = settings.value("m_fAreaAngle", 0).toDouble();
        this->m_caliLength = settings.value("m_caliLength", 100).toDouble();
        this->m_iMirrorToX = settings.value("m_iMirrorToX", 0).toInt();
        this->m_bMirror1Invert = settings.value("m_bMirror1Invert", 0).toInt();
        this->m_fMirror1Scale = settings.value("m_fMirror1Scale", 0).toDouble();
        this->m_fMirror1Barrel = settings.value("m_fMirror1Barrel", 0).toDouble();
        this->m_fMirror1ladder = settings.value("m_fMirror1ladder", 0).toDouble();
        this->m_bMirror2Invert = settings.value("m_bMirror2Invert", 0).toInt();
        this->m_fMirror2Scale = settings.value("m_fMirror2Scale", 0).toDouble();
        this->m_fMirror2Barrel = settings.value("m_fMirror2Barrel", 0).toDouble();
        this->m_fMirror2ladder = settings.value("m_fMirror2ladder", 0).toDouble();
        this->m_bUseCorFile = settings.value("m_bUseCorFile", 0).toInt();
        this->m_strCorFileName = settings.value("m_strCorFileName", "").toString();
        this->mK_value = settings.value("mK_value", 1000).toInt();
        settings.endGroup();
    }

    void writeToFile()
    {
        QSettings settings("config.ini", QSettings::IniFormat);
        settings.beginGroup(QString("CalibrateParam_%1").arg(m_sn_num));
        settings.setValue("m_fAreaSize",  this->m_fAreaSize);
        settings.setValue("m_fXOffSet",  this->m_fXOffSet);
        settings.setValue("m_fYOffSet",  this->m_fYOffSet);
        settings.setValue("m_fAreaAngle",  this->m_fAreaAngle);
        settings.setValue("m_caliLength",  this->m_caliLength);
        settings.setValue("m_iMirrorToX",  this->m_iMirrorToX);
        settings.setValue("m_bMirror1Invert",  this->m_bMirror1Invert);
        settings.setValue("m_fMirror1Scale",  this->m_fMirror1Scale);
        settings.setValue("m_fMirror1Barrel",  this->m_fMirror1Barrel);
        settings.setValue("m_fMirror1ladder",  this->m_fMirror1ladder);
        settings.setValue("m_bMirror2Invert",  this->m_bMirror2Invert);
        settings.setValue("m_fMirror2Scale",  this->m_fMirror2Scale);
        settings.setValue("m_fMirror2Barrel",  this->m_fMirror2Barrel);
        settings.setValue("m_fMirror2ladder",  this->m_fMirror2ladder);
        settings.setValue("m_bUseCorFile",  this->m_bUseCorFile);
        settings.setValue("m_strCorFileName",  this->m_strCorFileName);
        settings.setValue("mK_value",  this->mK_value);
        settings.endGroup();
    }

    QJsonObject toJsonObj() const
    {
        QJsonObject obj;
        obj["m_fAreaSize"] = m_fAreaSize;
        obj["m_fXOffSet"] = m_fXOffSet;
        obj["m_fYOffSet"] = m_fYOffSet;
        obj["m_fAreaAngle"] = m_fAreaAngle;
        obj["m_caliLength"] = m_caliLength;
        obj["m_iMirrorToX"] = m_iMirrorToX;
        obj["m_bMirror1Invert"] = m_bMirror1Invert;
        obj["m_fMirror1Scale"] = m_fMirror1Scale;
        obj["m_fMirror1Barrel"] = m_fMirror1Barrel;
        obj["m_fMirror1ladder"] = m_fMirror1ladder;
        obj["m_bMirror2Invert"] = m_bMirror2Invert;
        obj["m_fMirror2Scale"] = m_fMirror2Scale;
        obj["m_fMirror2Barrel"] = m_fMirror2Barrel;
        obj["m_fMirror2ladder"] = m_fMirror2ladder;
        obj["m_bUseCorFile"] = m_bUseCorFile;
        obj["m_strCorFileName"] = m_strCorFileName;
        obj["mK_value"] = mK_value;
        return obj;
    }

    void fromJsonObj(const QJsonObject& obj)
    {
        this->m_fAreaSize = obj["m_fAreaSize"].toDouble();
        this->m_fXOffSet = obj["m_fXOffSet"].toDouble();
        this->m_fYOffSet = obj["m_fYOffSet"].toDouble();
        this->m_fAreaAngle = obj["m_fAreaAngle"].toDouble();
        this->m_caliLength = obj["m_caliLength"].toInt();
        this->m_iMirrorToX = obj["m_iMirrorToX"].toInt();
        this->m_bMirror1Invert = obj["m_bMirror1Invert"].toInt();
        this->m_fMirror1Scale = obj["m_fMirror1Scale"].toDouble();
        this->m_fMirror1Barrel = obj["m_fMirror1Barrel"].toDouble();
        this->m_fMirror1ladder = obj["m_fMirror1ladder"].toDouble();
        this->m_bMirror2Invert = obj["m_bMirror2Invert"].toInt();
        this->m_fMirror2Scale = obj["m_fMirror2Scale"].toDouble();
        this->m_fMirror2Barrel = obj["m_fMirror2Barrel"].toDouble();
        this->m_fMirror2ladder = obj["m_fMirror2ladder"].toDouble();
        this->m_bUseCorFile = obj["m_bUseCorFile"].toInt();
        this->m_strCorFileName = obj["m_strCorFileName"].toString();
        this->mK_value = obj["mK_value"].toInt();
    }

    void operator =(const CalibrateParam& param){
        this->m_fAreaSize = param.m_fAreaSize;
        this->m_fXOffSet = param.m_fXOffSet;
        this->m_fYOffSet = param.m_fYOffSet;
        this->m_fAreaAngle = param.m_fAreaAngle;
        this->m_caliLength = param.m_caliLength;
        this->m_iMirrorToX = param.m_iMirrorToX;
        this->m_bMirror1Invert = param.m_bMirror1Invert;
        this->m_fMirror1Scale = param.m_fMirror1Scale;
        this->m_fMirror1Barrel = param.m_fMirror1Barrel;
        this->m_fMirror1ladder = param.m_fMirror1ladder;
        this->m_bMirror2Invert = param.m_bMirror2Invert;
        this->m_fMirror2Scale = param.m_fMirror2Scale;
        this->m_fMirror2Barrel = param.m_fMirror2Barrel;
        this->m_fMirror2ladder = param.m_fMirror2ladder;
        this->m_bUseCorFile = param.m_bUseCorFile;
        this->m_strCorFileName = param.m_strCorFileName;
        this->mK_value = param.mK_value;
    }

    bool operator ==(const CalibrateParam& param){
        bool rel = true;
        rel &= (this->m_fAreaSize == param.m_fAreaSize);
        rel &= (this->m_fXOffSet == param.m_fXOffSet);
        rel &= (this->m_fYOffSet == param.m_fYOffSet);
        rel &= (this->m_fAreaAngle == param.m_fAreaAngle);
        rel &= (this->m_caliLength == param.m_caliLength);
        rel &= (this->m_iMirrorToX == param.m_iMirrorToX);
        rel &= (this->m_bMirror1Invert == param.m_bMirror1Invert);
        rel &= (this->m_fMirror1Scale == param.m_fMirror1Scale);
        rel &= (this->m_fMirror1Barrel == param.m_fMirror1Barrel);
        rel &= (this->m_fMirror1ladder == param.m_fMirror1ladder);
        rel &= (this->m_bMirror2Invert == param.m_bMirror2Invert);
        rel &= (this->m_fMirror2Scale == param.m_fMirror2Scale);
        rel &= (this->m_fMirror2Barrel == param.m_fMirror2Barrel);
        rel &= (this->m_fMirror2ladder == param.m_fMirror2ladder);
        rel &= (this->m_bUseCorFile == param.m_bUseCorFile);
        rel &= (this->m_strCorFileName == param.m_strCorFileName);
        rel &= (this->mK_value == param.mK_value);
        return rel;
    }

    double fAreaSize() const
    {
        return m_fAreaSize;
    }

    void setfAreaSize(double fAreaSize)
    {
        m_fAreaSize = fAreaSize;
    }

    double fXOffSet() const
    {
        return m_fXOffSet;
    }

    void setfXOffSet(double fXOffSet)
    {
        m_fXOffSet = fXOffSet;
    }

    double fYOffSet() const
    {
        return m_fYOffSet;
    }

    void setfYOffSet(double fYOffSet)
    {
        m_fYOffSet = fYOffSet;
    }

    double caliLength() const
    {
        return m_caliLength;
    }

    void setcaliLength(double caliLength)
    {
        m_caliLength = caliLength;
    }

    double fAreaAngle() const
    {
        return m_fAreaAngle;
    }

    void setfAreaAngle(double fAreaAngle)
    {
        m_fAreaAngle = fAreaAngle;
    }

    int iMirrorToX() const
    {
        return m_iMirrorToX;
    }

    void setiMirrorToX(int iMirrorToX)
    {
        m_iMirrorToX = iMirrorToX;
    }

    int bMirror1Invert() const
    {
        return m_bMirror1Invert;
    }

    void setbMirror1Invert(int bMirror1Invert)
    {
        m_bMirror1Invert = bMirror1Invert;
    }

    double fMirror1Scale() const
    {
        return m_fMirror1Scale;
    }

    void setfMirror1Scale(double fMirror1Scale)
    {
        m_fMirror1Scale = fMirror1Scale;
    }

    double fMirror1Barrel() const
    {
        return m_fMirror1Barrel;
    }

    void setfMirror1Barrel(double fMirror1Barrel)
    {
        m_fMirror1Barrel = fMirror1Barrel;
    }

    double fMirror1ladder() const
    {
        return m_fMirror1ladder;
    }

    void setfMirror1ladder(double fMirror1ladder)
    {
        m_fMirror1ladder = fMirror1ladder;
    }

    int bMirror2Invert() const
    {
        return m_bMirror2Invert;
    }

    void setbMirror2Invert(int bMirror2Invert)
    {
        m_bMirror2Invert = bMirror2Invert;
    }

    double fMirror2Scale() const
    {
        return m_fMirror2Scale;
    }

    void setfMirror2Scale(double fMirror2Scale)
    {
        m_fMirror2Scale = fMirror2Scale;
    }

    double fMirror2Barrel() const
    {
        return m_fMirror2Barrel;
    }

    void setfMirror2Barrel(double fMirror2Barrel)
    {
        m_fMirror2Barrel = fMirror2Barrel;
    }

    double fMirror2ladder() const
    {
        return m_fMirror2ladder;
    }

    void setfMirror2ladder(double fMirror2ladder)
    {
        m_fMirror2ladder = fMirror2ladder;
    }

    int bUseCorFile() const
    {
        return m_bUseCorFile;
    }

    void setbUseCorFile(int b)
    {
        m_bUseCorFile = b;
    }

    QString strCorFileName()
    {
        return m_strCorFileName;
    }

    void setStrCorFileName(QString n)
    {
        m_strCorFileName = n;
    }

    int k_value()
    {
        return mK_value;
    }

    void setK_value(int k)
    {
        mK_value = k;
    }

private:
    QString m_sn_num;//设备序列号
    //外观
    double m_fAreaSize = 0; //  区域 mm
    double m_fXOffSet = 0;  //  X偏移 mm
    double m_fYOffSet = 0;  //  Y偏移 mm
    double m_fAreaAngle = 0;//  角度 角度
    double m_caliLength = 0;//  测试标定矩形宽
    int m_iMirrorToX = 0;//振镜1 或 振镜2 = X
    //振镜1
    int  m_bMirror1Invert = 0; // 振镜1 是否反转
    double m_fMirror1Scale = 0;  // 比例
    double m_fMirror1Barrel = 0;  // 椭圆 桶型矫正
    double m_fMirror1ladder = 0; // 梯形
    //振镜2
    int  m_bMirror2Invert = 0; // 振镜2 是否反转
    double m_fMirror2Scale = 0;  // 比例
    double m_fMirror2Barrel = 0; // 椭圆 桶型矫正
    double m_fMirror2ladder = 0; // 梯形

    int  m_bUseCorFile = 0;//是否使用校正文件
    QString m_strCorFileName = "";//校正文件
    int mK_value = 1000;//K值
};

#endif // CALIBRATEPARAM_H
